﻿using ExpressesClass.Model;
using System;
using System.Drawing;
using System.Drawing.Printing;
using ZXing;
using ZXing.Common;

namespace ExpressesClass.IPrints
{
    public class SfPrint130 : IPrint
    {
        private Kddh kddh;
        private int printCur;
        private DateTime now;

        private int _w, _5, _8, _10, _15, _20, _25, _30, _35, _60;
        public SfPrint130(Kddh kddh, int printCur, DateTime now)
        {
            this.kddh = kddh;
            this.printCur = printCur;
            this.now = now;
            _w = 290;
            _5 = 20;
            _8 = 36;
            _10 = 40;
            _15 = 60;
            _20 = 80;
            _25 = 100;
            _30 = 120;
            _35 = 140;
            _60 = 240;
        }
        public void Print(string printer)
        {
            PrintDocument printDoc = new PrintDocument();
            printDoc.PrinterSettings.PrinterName = printer;
            printDoc.DefaultPageSettings.PaperSize = new PaperSize("SfSize", 290, 400);
            printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage); //打印事件 
            printDoc.Print();
        }
        private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            //设置高质量插值法   
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度   
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //消除锯齿
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            //二维码
            PrintQRMailNo(g, _w - _30 - 50 - 10, _30 + _8 + _25 + _15 - 10, _35, _35);
            //横向面单号
            PrintHorMailNo(g, -10, _10 + 3, _60 + _20, _10 + 5);
            //竖向面单号
            PrintVerMailNo(g, _w - 50, _30 + _8 - 10, 50, _60 + _20);

            //时效名称
            PrintProName(g, _w - 150, 5, 120, 21);
            //时间
            PrintPrintTime(g, (_w - _25) / 2, _10 - 10, _35, _5);
            //运单号
            PrintMailNo(g, 40, 93, _w, _15);
            //路由标签
            PrintRouteLable(g, 5, _30, _w, _8);
            //收
            PrintReceive(g, 5, _30 + _8 + 4, 22, 22);
            //收件人
            PrintReceiveMan(g, 32, _30 + _8 + 4, _w - 55, _8);
            //判断是否存在客户
            if ((kddh.receiveCompany ?? "") != "")
            {
                //客户
                PrintReceiveCompany(g, 5, _30 + _8 + 28, _w - 55, _20);
                //收件地址
                PrintReceiveAddress(g, 5, _30 + _8 + 43, _w - 55, _20);
            }
            else
            {
                //收件地址
                PrintReceiveAddress(g, 5, _30 + _8 + 28, _w - 55, _20);
            }
            //寄
            PrintSend(g, 5, _30 + _8 + _25 + 4, 22, 22);
            //寄件人
            PrintSendMan(g, 32, _30 + _8 + _25 + 2, _w - 85, _8);
            //寄件地址
            PrintSendAddress(g, 32, _30 + _8 + _25 + 20, _w - 85, _8);
            //已检视
            PrintLook(g, _w - _30 - 50 - _15, _30 + _8 + _25 + _15, _35, _8);
            //入港映射码
            PrintCodingMapping(g, 3, _30 + _8 + _25 + _15 + _8 + _8, _35, _20);
            //位置
            PrintWz(g, 3, _30 + _8 + _25 + _15 + _30, _w - 55, _8);
            //备注
            PrintRemark(g, 3, _30 + _8 + _25 + _15 + _30 + 15, _w - 55, _15);
            //边框
            PrintRectangle(g);
        }
        private void PrintRectangle(Graphics g)
        {
            Pen pen = new Pen(Brushes.Black, 1);
            //横
            g.DrawLine(pen, 0, _10, _w, _10);
            g.DrawLine(pen, 0, _30, _w, _30);
            g.DrawLine(pen, 0, _30 + _8, _w, _30 + _8);
            g.DrawLine(pen, 0, _30 + _8 + _25, _w - 50, _30 + _8 + _25);
            g.DrawLine(pen, 0, _30 + _8 + _25 + _15, _w - 50, _30 + _8 + _25 + _15);
            g.DrawLine(pen, 0, _30 + _8 + _25 + _15 + _8, _w - _30 - 50, _30 + _8 + _25 + _15 + _8);
            g.DrawLine(pen, 0, _30 + _8 + _25 + _15 + _8 + _8, _w - _30 - 50, _30 + _8 + _25 + _15 + _8 + _8);
            g.DrawLine(pen, 0, _30 + _8 + _25 + _15 + _30, _w - 50, _30 + _8 + _25 + _15 + _30);
            //竖
            g.DrawLine(pen, _w - _30 - 50, _30 + _8 + _25 + _15, _w - _30 - 50, _30 + _8 + _25 + _15 + _30);
            g.DrawLine(pen, _w - 50, _30 + _8, _w - 50, _30 + _8 + _25 + _15 + _30 + _15);
        }
        private void PrintPrintTime(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 5, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("打印时间:" + now.ToString("yyyy/MM/dd HH:mm:ss"), font, Brushes.Black, rec);
        }
        private void PrintHorMailNo(Graphics g, int x, int y, int w, int h)
        {
            EncodingOptions encodeOption = new EncodingOptions();
            encodeOption.Width = 200;
            encodeOption.Height = 40;
            BarcodeWriter wr = new BarcodeWriter();
            wr.Options = encodeOption;
            wr.Format = BarcodeFormat.CODE_128;
            encodeOption.PureBarcode = true;
            System.Threading.Thread.Sleep(100);

            string mailNo;
            if (int.Parse(kddh.itemCount) > 1 && printCur > 1 && kddh.mailno_zd != null && kddh.mailno_zd.Split(',').Length > (printCur - 2))
            {
                mailNo = kddh.mailno_zd.Split(',')[printCur - 2];
            }
            else
            {
                mailNo = kddh.mailNo;
            }
            Bitmap img = wr.Write(mailNo);
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintVerMailNo(Graphics g, int x, int y, int w, int h)
        {
            Bitmap bitmap = new Bitmap(200, 40);

            EncodingOptions encodeOption = new EncodingOptions();
            encodeOption.Width = 200;
            encodeOption.Height = 40;
            BarcodeWriter wr = new BarcodeWriter();
            wr.Options = encodeOption;
            wr.Format = BarcodeFormat.CODE_128;
            encodeOption.PureBarcode = true;
            System.Threading.Thread.Sleep(100);

            string mailNo;
            if (int.Parse(kddh.itemCount) > 1 && printCur > 1 && kddh.mailno_zd != null && kddh.mailno_zd.Split(',').Length > (printCur - 2))
            {
                mailNo = kddh.mailno_zd.Split(',')[printCur - 2];
            }
            else
            {
                mailNo = kddh.mailNo;
            }
            Bitmap img = wr.Write(mailNo);

            Graphics tempG = Graphics.FromImage(bitmap);
            tempG.DrawImage(img, 0, 0);

            bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
            g.DrawImage(bitmap, x, y, w, h);
        }
        private void PrintMailNo(Graphics g, int x, int y, int w, int h)
        {
            Font fontNo = new Font("微软雅黑", 10, FontStyle.Regular);
            Font font = new Font("微软雅黑", 7, FontStyle.Regular);

            string strNo = printCur.ToString() + "/" + kddh.itemCount;
            RectangleF recNo = new RectangleF(x, y, w, h);
            g.DrawString(strNo, fontNo, Brushes.Black, recNo);

            if (int.Parse(kddh.itemCount) > 1)
            {
                string mailNo = kddh.mailNo.Length > 11 ? kddh.mailNo.Substring(0, 2) + "  " + kddh.mailNo.Substring(2, 3) + "  " + kddh.mailNo.Substring(5, 3) + "  " + kddh.mailNo.Substring(8, 3) + "  " + kddh.mailNo.Substring(11) : kddh.mailNo;
                RectangleF recMailNo = new RectangleF(x + _10, y, w, h);
                g.DrawString("母单号  " + mailNo, font, Brushes.Black, recMailNo);

                if (printCur > 1 && kddh.mailno_zd != null && kddh.mailno_zd.Split(',').Length > (printCur - 2))
                {
                    string zd = kddh.mailno_zd.Split(',')[printCur - 2];
                    string mailNo_Zd = zd.Length > 11 ? zd.Substring(0, 2) + "  " + zd.Substring(2, 3) + "  " + zd.Substring(5, 3) + "  " + zd.Substring(8, 3) + "  " + zd.Substring(11) : zd;
                    RectangleF recMailNo_Zd = new RectangleF(x + _10, y + 12, w, h);
                    g.DrawString("子单号  " + mailNo_Zd, font, Brushes.Black, recMailNo_Zd);
                }
            }
            else
            {
                string mailNo = kddh.mailNo.Length > 11 ? kddh.mailNo.Substring(0, 2) + "  " + kddh.mailNo.Substring(2, 3) + "  " + kddh.mailNo.Substring(5, 3) + "  " + kddh.mailNo.Substring(8, 3) + "  " + kddh.mailNo.Substring(11) : kddh.mailNo;
                RectangleF recMailNo = new RectangleF(x + _10, y, w, h);
                g.DrawString("运单号  " + mailNo, font, Brushes.Black, recMailNo);
            }
        }
        private void PrintLogo(Graphics g, int x, int y, int w, int h)
        {
            Bitmap img = Resource.sflogo;
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintQiao(Graphics g, int x, int y, int w, int h)
        {
            Bitmap img = Resource.qiao;
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintProName(Graphics g, int x, int y, int w, int h)
        {
            string str = (string.IsNullOrEmpty(kddh.proCode) ? "" : kddh.proCode) + " " + (string.IsNullOrEmpty(kddh.proName) ? "" : kddh.proName);
            Font font = new Font("微软雅黑", 12, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(str, font, Brushes.Black, rec);
        }
        private void PrintRouteLable(Graphics g, int x, int y, int w, int h)
        {
            Font font = new Font("微软雅黑", 18, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.destRouteLabel, font, Brushes.Black, rec);
        }
        private void PrintDestTeamCode(Graphics g, int x, int y, int w, int h)
        {
            Font font = new Font("微软雅黑", 20, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.destTeamCode, font, Brushes.LightGray, rec);
        }
        private void PrintCodingMapping(Graphics g, int x, int y, int w, int h)
        {
            Font font = new Font("微软雅黑", 20, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.codingMapping, font, Brushes.Black, rec);
        }
        private void PrintSourceTransferCode(Graphics g, int x, int y, int w, int h)
        {
            Font font = new Font("微软雅黑", 20, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.sourceTransferCode, font, Brushes.Black, rec);
        }
        private void PrintAbFlag(Graphics g, int x, int y, int w, int h)
        {
            Bitmap img = Resource.A;
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintCodingMappingOut(Graphics g, int x, int y, int w, int h)
        {
            Font font = new Font("微软雅黑", 20, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.codingMappingOut, font, Brushes.Black, rec);
        }
        private void PrintReceive(Graphics g, int x, int y, int w, int h)
        {
            Bitmap img = Resource.so;
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintSend(Graphics g, int x, int y, int w, int h)
        {
            Bitmap img = Resource.ji;
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintIcon(Graphics g, int x, int y, int w, int h)
        {
            if (kddh.printIcon == "00001000")
            {
                Bitmap img = Resource.PI5;
                g.DrawImage(img, x, y, w, h);
            }
        }
        private void PrintReceiveMan(Graphics g, int x, int y, float w, float h)
        {
            string phone = kddh.receiveManPhone.Length > 7 ? kddh.receiveManPhone.Substring(0, 3) + "****" + kddh.receiveManPhone.Substring(7) : kddh.receiveManPhone;
            Font font = new Font("微软雅黑", 11, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.receiveMan + " " + phone, font, Brushes.Black, rec);
        }
        private void PrintReceiveAddress(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 8, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("地址:" + kddh.receiveManAddress, font, Brushes.Black, rec);
        }
        private void PrintReceiveCompany(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 8, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("客户:" + kddh.receiveCompany, font, Brushes.Black, rec);
        }
        private void PrintSendMan(Graphics g, int x, int y, float w, float h)
        {
            string phone = kddh.sendManPhone.Length > 7 ? kddh.sendManPhone.Substring(0, 3) + "****" + kddh.sendManPhone.Substring(7) : kddh.sendManPhone;
            Font font = new Font("微软雅黑", 8, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.sendMan + " " + phone, font, Brushes.Black, rec);
        }
        private void PrintSendAddress(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 7, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("地址:" + kddh.sendManAddress, font, Brushes.Black, rec);
        }
        private void PrintQRMailNo(Graphics g, int x, int y, int w, int h)
        {
            EncodingOptions encodeOption = new EncodingOptions();
            encodeOption.Width = 200;
            encodeOption.Height = 200;
            BarcodeWriter wr = new BarcodeWriter();
            wr.Options = encodeOption;
            wr.Format = BarcodeFormat.QR_CODE;
            encodeOption.PureBarcode = false;
            System.Threading.Thread.Sleep(100);
            Bitmap img = wr.Write(kddh.twoDimensionCode);
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintRemark(Graphics g, float x, float y, float w, float h)
        {
            string str = kddh.remark;
            Font font = new Font("微软雅黑", 6, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("序号:" + printCur + ";" + str, font, Brushes.Black, rec);
        }
        private void PrintWz(Graphics g, float x, float y, float w, float h)
        {
            string str = kddh.wzh;
            Font font = new Font("微软雅黑", 10, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(str, font, Brushes.Black, rec);
        }
        private void PrintLook(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 10, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("已检视", font, Brushes.Black, rec);
        }
    }
}
